<template>
  <div class="login">
    <div class="info">
      <p>欢迎使用考试汇</p>
      <!-- {{$store.state.user_id}} -->
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="loginForm"
        label-width="auto"
      >
        <el-form-item label="账号" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>

        <div class="login-but" @click="login('loginForm')">登录</div>
      </el-form>
    </div>
  </div>
</template>

<script>
import "../assets/less/login.less";
export default {
  name: "Login",
  data() {
    return {
      form: {
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
      },
    };
  },

  methods: {
    //表单提交-----登录
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 处理请求参数
          var request = {
            userName: this.form.name,
          };
          // 请求数据
          this.$http.post("/student/start", request).then((res) => {
            if (res.code == 200) {
              var data = res.data;
              //登录成功跳转
              localStorage.setItem("_token", data.token);
              this.$router.push("/examdetail");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

